package com.rongshu.view.userInfo.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Constant;
import com.rongshu.utils.DateUtils;
import com.rongshu.utils.GlideUtils;
import com.rongshu.view.userInfo.activity.GiveLikeUserAcitivity;
import com.rongshu.view.userInfo.entity.GiveLikeMessage;
import com.rongshu.view.userInfo.listener.OnMsgFunctionClickListener;
import com.utils.StringUtils;
import com.widget.CircleImageView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GiveLikeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext = null;
    private List<GiveLikeMessage.ResponseBean> giveLikeMessageList = null;
    //interface
    private OnMsgFunctionClickListener mOnMsgFunctionListener = null;

    public GiveLikeAdapter(Context context, List<GiveLikeMessage.ResponseBean> giveLikeMessageList) {
        this.mContext = context;
        this.giveLikeMessageList = giveLikeMessageList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.adapter_give_like_item_view, viewGroup, false);
        return new GiveLikeViewHolder(view);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {
        final GiveLikeMessage.ResponseBean bean = giveLikeMessageList.get(i);
        GiveLikeViewHolder holder = (GiveLikeViewHolder) viewHolder;
        try {
            //点赞人数据集合
            final List<GiveLikeMessage.ResponseBean.LikeUserListBean> userBeans = bean.getLikeUserList();
            //点赞人数
            int likeCount = bean.getLikeCount();
            if (userBeans != null) {
                if (likeCount >= 1) {
                    //头像
                    GlideUtils.loadImageView(mContext, holder.iv_head_icon, userBeans.get(0).getAvatar(), R.drawable.user_head_icon, R.drawable.user_head_icon);
                    holder.iv_vip.setVisibility(userBeans.get(0).isVip() ? View.VISIBLE : View.GONE);
                    //来自人
                    holder.txt_bi_name.setText(userBeans.get(0).getBi_name());
                }
                //日期
                final String time = bean.getCreate_datetime();
                holder.txt_time.setText(DateUtils.ConvertDate(time));
                //点赞提示
                holder.txt_like_hint.setText("等" + likeCount + "人赞了你的作品");
                //内容
                holder.txt_article_title.setText(StringUtils.decoderMsg(bean.getTitle()));
                //其他点赞人头像
                if (likeCount > 1) {
                    holder.layout_user_head_portrait.setVisibility(View.VISIBLE);
                    holder.layout_user_head_portrait.removeAllViews();
                    for (int j = 1; j < userBeans.size(); j++) {
                        View view = LayoutInflater.from(mContext).inflate(R.layout.adapter_user_head, holder.layout_user_head_portrait, false);
                        CircleImageView circleImageView = view.findViewById(R.id.iv_head_icon);
                        ImageView iv_vip = view.findViewById(R.id.iv_vip);
                        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
                        GiveLikeMessage.ResponseBean.LikeUserListBean userBean = userBeans.get(j);
                        if (j < 5) {
                            GlideUtils.loadImageView(mContext, circleImageView, userBean.getAvatar(), R.drawable.user_head_icon, R.drawable.user_head_icon);
                            iv_vip.setVisibility(userBean.isVip() ? View.VISIBLE : View.GONE);
                            if (j == 1) {
                                holder.layout_user_head_portrait.addView(view, params);
                            } else {
                                params.leftMargin = mContext.getResources().getDimensionPixelOffset(R.dimen.dd_dimen_22px);
                                holder.layout_user_head_portrait.addView(view, params);
                            }
                            final int finalJ = j;
                            circleImageView.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    enterOtherHomePager(userBeans.get(finalJ).getUser_id(), false, bean.getContribute_id());
                                }
                            });
                        }
                    }
                    if (likeCount > userBeans.size()) {
                        View view = LayoutInflater.from(mContext).inflate(R.layout.adapter_user_head, holder.layout_user_head_portrait, false);
                        CircleImageView circleImageView = view.findViewById(R.id.iv_head_icon);
                        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
                        circleImageView.setImageResource(R.drawable.default_more_user_head_icon);
                        params.leftMargin = mContext.getResources().getDimensionPixelOffset(R.dimen.dd_dimen_22px);
                        holder.layout_user_head_portrait.addView(view, params);
                        circleImageView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                enterOtherHomePager(0, true, bean.getContribute_id());
                            }
                        });
                    }
                }
                //listener
                holder.layout_content.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mOnMsgFunctionListener != null) {
                            mOnMsgFunctionListener.onItemClick(i);
                        }
                    }
                });
                holder.iv_head_icon.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        enterOtherHomePager(userBeans.get(0).getUser_id(), false, bean.getContribute_id());
                    }
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 进入他人主页或则我的主页，或则所有点过赞的人
     */
    private void enterOtherHomePager(int userid, boolean isToSeeAll, int contribute_id) {
        if (isToSeeAll) {
            //进入所有点赞的人
            Intent intent = new Intent(mContext, GiveLikeUserAcitivity.class);
            intent.putExtra(Constant.INTENT_DATA, contribute_id);
            mContext.startActivity(intent);
        } else {
            if (mOnMsgFunctionListener != null) {
                mOnMsgFunctionListener.onUserHeaderClick(userid);
            }
        }
    }

    @Override
    public int getItemCount() {
        return giveLikeMessageList == null ? 0 : giveLikeMessageList.size();
    }

    public class GiveLikeViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.layout_content)
        RelativeLayout layout_content;
        @BindView(R.id.iv_head_icon)
        CircleImageView iv_head_icon;
        @BindView(R.id.iv_vip)
        ImageView iv_vip;
        @BindView(R.id.txt_bi_name)
        TextView txt_bi_name;
        @BindView(R.id.txt_time)
        TextView txt_time;
        @BindView(R.id.txt_like_hint)
        TextView txt_like_hint;
        @BindView(R.id.txt_article_title)
        TextView txt_article_title;
        @BindView(R.id.layout_user_head_portrait)
        LinearLayout layout_user_head_portrait;

        public GiveLikeViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public void setOnItemClickListener(OnMsgFunctionClickListener listener) {
        this.mOnMsgFunctionListener = listener;
    }
}
